package com.uchengwang.ui.cm_ui.rychat.manager;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.RyConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.listener.IHttpManager;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.functions.http.listener.OnHttpResultSingleListener;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.cm_ui.cycle.bean.RyGroupBean;
import com.uchengwang.ui.cm_ui.rychat.bean.InviteUserBean;
import com.uchengwang.ui.cm_ui.rychat.bean.RyGroupListBean;
import com.uchengwang.ui.cm_ui.rychat.bean.RyUserBean;
import com.uchengwang.ui.cm_ui.rychat.bean.RyUserListBean;
import com.uchengwang.ui.cm_ui.umsg.http.ClubHttpManager;
import com.uchengwang.ui.cm_ui.uu.bean.RyGroupInviteBean;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.List;

/**
 * Created by U城网 on 2017/10/21.
 * 我们自己后台
 * 融云相关网络请求
 */

public class RyGroupManager {

    private RyGroupManager(){}
    private static RyGroupManager manager;

    public static RyGroupManager getManager() {
        if (manager == null){
            manager = new RyGroupManager();
        }
        return manager;
    }

    /**
     * 获取我的群 加入或者创建
     * @param referType 可以为0
     * */
    public void getMyGroups(Activity activity,int page, int perPage, int referType, OnHttpResultListener<RyGroupBean> listener){
        if (!UCommonUtil.getUtil().isLogin()){
            return;
        }
        getGroups(activity, 1,page, perPage, referType, 0, 0, "", listener);
    }

    /**
     * 获取相关校吧的群组
     * */
    public void getBaGroup(Activity activity, int page, int perPage, int groupType, long orgId, final OnHttpResultListener<RyGroupBean> listener){
        getGroups(activity, 2, page, perPage, groupType, orgId, UChengApplication.uniId, "", listener);

    }

    /**
     * 获取相关社圈的群组
     * */
    public void getCycleGroup(Activity activity, int page, int perPage, long cycleId, final OnHttpResultListener<RyGroupBean> listener){
        getGroups(activity, 2, page, perPage, 8, cycleId, 0, "", listener);

    }



    /**
     * 获取群列表 [获取群的底层基本方法]
     * @param type 1获取与我有关的群  2是获取群列表
     * @param uniId 学校ID
     * @param referType 群类别 U学 U赛 班级群 社团群等 1.Publish群; 2.好友群; 3.社团群; 4.兴趣圈群; 5.班级群; 6.纳新群 不填获取全部类别 7老乡群 8社圈群
     * @param keyword 关键字
     * @param referId 可以是某个兴趣圈的ID 社团ID  可以为0
     * */
    public void getGroups(Activity activity, int type, int page, int perPage, int referType, long referId, long uniId, String keyword, final OnHttpResultListener<RyGroupBean> listener){
        MapRequest map = new MapRequest();
        IHttpManager.RequestMethod method = null;
        String url = "";

        switch (type){
            case 1://我加入或者创建的群
                method = RyConfig.MyGroup.method;
                url = RyConfig.MyGroup.URL;
                break;
            case 2:
                method = RyConfig.Groups.method;
                url = RyConfig.Groups.URL;
                break;
        }

//        if (uniId >0){
//            map.put(BaseConfig.UNI_ID, uniId);
//        }
        if (referType >0){
            map.put(BaseConfig.REFER_TYPE, referType);
        }
        if (!TextUtils.isEmpty(keyword)){
            map.put(BaseConfig.KEY_WORD, keyword);
        }
        if ( referId >0){
            map.put(BaseConfig.REFER_ID, referId);
        }
        map.put(BaseConfig.PAGE, page);
        map.put(BaseConfig.PERPAGE, perPage);
        HttpManager.newInstance(activity).query(method, url, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                listener.onResult(status, "", 0,null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                if (status ==0 && !TextUtils.isEmpty(data) && !data.equals("[]")){
                    RyGroupListBean bean = JsonUtils.getManager().stringToBean(data, RyGroupListBean.class);
                    //为了方便在我的界面展示群数量  将群的数量放在status里
                    listener.onResult(status, bean.getTotal()+"", bean.getTotalPage(),bean.getData());

                }else {
                    listener.onResult(status, "", 0, null);
                }
            }
        });
    }


    /**
     * 获取某个群的群成员
     * */
    public void getGroupMembers(Activity activity, long groupId, int page, int perPage, final OnHttpResultListener<RyUserBean> listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.GROUP_ID, groupId);
        map.put(BaseConfig.PAGE, page);
        map.put(BaseConfig.PERPAGE, perPage);
        HttpManager.newInstance(activity).query(RyConfig.GroupMember.mthod, RyConfig.GroupMember.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                if (status ==0 && !TextUtils.isEmpty(data) && !data.equals("[]")){
                    RyUserListBean bean = JsonUtils.getManager().stringToBean(data, RyUserListBean.class);
                    listener.onResult(status, msg, bean.getTotalPage(),bean.getData());
                }else {
                    listener.onResult(status, msg, 0, null);
                }
            }
        });
    }

    /**
     * 退出某个群/踢一个人出群
     * @param isExit true 是自己退出一个群 false是被踢出一个群
     * @param uid isExit为true 退出群 uid可为0  false是被踢出人的ID
     * */
    public void exitGroup(Activity activity, boolean isExit , long groupId, long uid, final OnResultListener listener){
        LoadingUtils.getUtils().loading(activity);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.GROUP_ID, groupId);

        if (uid >0){
            map.put(BaseConfig.KICK_UID, uid);//退出群 uid不用
        }

        IHttpManager.RequestMethod method = null;
        String url = "";
        if (isExit){
            //退出一个群
            method = RyConfig.ExitGroup.method;
            url = RyConfig.ExitGroup.URL;
        }else {
            method = RyConfig.KickGroup.method;
            url = RyConfig.KickGroup.URL;
        }
        HttpManager.newInstance(activity).query(method, url, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }
        });
    }

    /**
     * 邀请人加入群
     * */
    public void inviteMember(Activity activity, List<InviteUserBean> list, final OnResultListener listener){
        LoadingUtils.getUtils().loading(activity);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.LIST, list);
        HttpManager.newInstance(activity).query(RyConfig.InviteGroup.method_post, RyConfig.InviteGroup.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }
        });
    }

    /**
     * 查看一个群的信息
     * */
    public void getGroupInfo(Activity activity, long groupId, final OnHttpResultSingleListener<RyGroupBean> listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.GROUP_ID, groupId);
        HttpManager.newInstance(activity).query(RyConfig.Groups.method, RyConfig.Groups.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                listener.onResult(status, msg, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                if (status ==0 && !TextUtils.isEmpty(data)){
                    RyGroupBean bean = JsonUtils.getManager().stringToBean(data, RyGroupBean.class);
                    listener.onResult(status, msg, bean);
                }else {
                    listener.onResult(status, msg, null);
                }
            }
        });
    }

    /**
     * 申请加入一个群
     * */
    public void applyGroup(Activity activity, String applyMsg, long groupId,  String groupName, final OnResultListener listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.APPLY_MSG, applyMsg);
        map.put(BaseConfig.GROUP_ID, groupId);
        map.put(BaseConfig.GROUP_NAME_UCW, groupName);
        HttpManager.newInstance(activity).query(RyConfig.ApplyGroup.method, RyConfig.ApplyGroup.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                listener.onResult(status, msg, 0, null);
            }
        });
    }

    /**
     * 加入一个群[无需申请]
     * */
    public void joinGroupNotApply(Activity activity, long groupId, final OnResultListener listener){
        LoadingUtils.getUtils().loading(activity);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.GROUP_ID, groupId);
        HttpManager.newInstance(activity).query(RyConfig.JoinGroup.method, RyConfig.JoinGroup.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }
        });
    }


    /**
     * 对一个申请做处理/对一个邀请做处理
     * isInvite true 邀请 false申请
     * @param id 邀请信息/申请信息ID
     * @param isAccept 同意或者接受
     * */
    public void conductGroupNotify(Activity activity, boolean isInvite, long id,  boolean isAccept, final OnResultListener listener){
        if (isInvite){
            conductInvite(activity, id, isAccept, listener);
        }else {
            conductApply(activity, id, isAccept, listener);
        }
    }

    /**
     * 用户对一个群邀请做处理
     * @param inviteId 邀请信息ID
     * @param isAccept
     * */
    public void conductInvite(Activity activity, long inviteId, boolean isAccept, final OnResultListener listener){
        LoadingUtils.getUtils().loading(activity);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.INVITE_ID, inviteId);
        map.put(BaseConfig.ACCEPT, isAccept);
        HttpManager.newInstance(activity).query(RyConfig.InviteGroup.method_put, RyConfig.InviteGroup.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, data);
            }
        });
    }

    /**
     * 群主对一个申请加群做处理
     * @param applyId 后台返回的信息ID
     * @param isAccept 接受或者拒绝
     * */
    public void conductApply(Activity activity, long applyId, boolean isAccept, final OnResultListener listener){
        LoadingUtils.getUtils().loading(activity);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.APPLY_ID, applyId);
        map.put(BaseConfig.ACCEPT, isAccept);
        HttpManager.newInstance(activity).query(RyConfig.ApplyGroup.method_put, RyConfig.ApplyGroup.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, data);
            }
        });

    }

    /**
     * 群主解散一个群
     * */
    public void deleteGroup(Activity activity, long kickUid, long groupId, final OnResultListener listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.KICK_UID, kickUid);
        map.put(BaseConfig.GROUP_ID, groupId);
        HttpManager.newInstance(activity).query(RyConfig.DeleteGroup.method_delete, RyConfig.DeleteGroup.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                listener.onResult(status, msg, 0, data);
            }
        });
    }

    /**
     * 获取我的邀请和申请未读数
     * */
    public void getMyUnread(Activity activity, final OnResultListener listener){
        MapRequest map = new MapRequest();
        HttpManager.newInstance(activity).query(RyConfig.UnreadGroup.method, RyConfig.UnreadGroup.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                if (status == 0 && !TextUtils.isEmpty(data)){
                    JSONObject jsonObject = JsonUtils.getManager().getJSONObject(data);
                    int applyUnread = jsonObject.getIntValue("applyUnread");//申请未读数
                    int inviteUnread = jsonObject.getIntValue("inviteUnread");//邀请未读数
                    listener.onResult(status, msg, applyUnread+inviteUnread, data);
                }else {
                    listener.onResult(status, msg, 0, null);
                }
            }
        });
    }

    /**
     * 群主查看收到的用户申请加群
     * */
    public void checkApply(Activity activity, final OnHttpResultListener<RyGroupInviteBean> listener){
        HttpManager.newInstance(activity).query(RyConfig.ApplyAdminGroup.method_get, RyConfig.ApplyAdminGroup.URL, null, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                if (status ==0 && !TextUtils.isEmpty(data) && !data.equals("[]")){
                    List<RyGroupInviteBean> list = JsonUtils.getManager().parseList(data, RyGroupInviteBean.class);
                    listener.onResult(status, msg, 0,list);
                }else {
                    listener.onResult(status, msg, 0, null);
                }
            }
        });
    }

    /**
     * 用户查看收到的群邀请
     * */
    public void checkInvite(Activity activity, final OnHttpResultListener<RyGroupInviteBean> listener){
        HttpManager.newInstance(activity).query(RyConfig.MyAcceptInvite.method, RyConfig.MyAcceptInvite.URL, null, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                listener.onResult(status, msg, 0,null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("ryjjf", "stutas="+status+"=msg="+msg);
                if (status ==0 && !TextUtils.isEmpty(data) && !data.equals("[]")){
                    List<RyGroupInviteBean> list = JsonUtils.getManager().parseList(data, RyGroupInviteBean.class);
                    listener.onResult(status, msg, 0, list);
                }else {
                    listener.onResult(status, msg, 0,null);
                }
            }
        });
    }

    /**
     * 修改群信息
     * */
    public void changeGroupInfo(Activity activity, MapRequest map, final OnResultListener listener){
        LoadingUtils.getUtils().loading(activity);
        HttpManager.newInstance(activity).query(RyConfig.Groups.method_put, RyConfig.Groups.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("ryjjf", "status="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("ryjjf", "status="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }
        });
    }

    /**
     * 转让群
     * @param tranferUid 被转让人的ID
     * */
    public void tranferGroup(Activity activity, long groupId, long tranferUid, final OnResultListener listener){
        LoadingUtils.getUtils().loading(activity);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.GROUP_ID, groupId);
        map.put(BaseConfig.TRANFER_UID, tranferUid);
        HttpManager.newInstance(activity).query(RyConfig.TranferGroup.method, RyConfig.TranferGroup.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("ryjjf", "status="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("ryjjf", "status="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }
        });
    }

   /**
    * 是否在兴趣圈/老乡会校吧里创建过群组
    * */
    public void isCreateGroupInBa(Activity activity, final int orgType, final ClubHttpManager.CreateOrNotListener listener){
        getMyGroups(activity, 1, 999, 0, new OnHttpResultListener<RyGroupBean>() {
            @Override
            public void onResult(int status, String msg, int total, List<RyGroupBean> list) {
                if (list != null && list.size() >0){
                    for (RyGroupBean b: list){
                        if (b.getOrgType()== orgType){
                            listener.create(0, true);
                            return;
                        }
                    }
                    listener.create(status, false);
                }else {
                    listener.create(status, false);
                }
            }
        });
    }
}
